#include <stdafx.h>

void CSystem::sleep(s32 _milliSeconds)
{
#ifdef WIN32
	Sleep(_milliSeconds);
#else // linux
	usleep(_milliSeconds * 1000);
#endif // WIN32
}

CPair<u32, u32> CSystem::getMemoryUsage()
{
#ifdef WIN32
	MEMORYSTATUS MemoryStatus;
	MemoryStatus.dwLength = sizeof(MEMORYSTATUS);

	GlobalMemoryStatus(&MemoryStatus);

	return CPair<u32, u32>(MemoryStatus.dwAvailPhys >> 10, MemoryStatus.dwTotalPhys >> 10);
#else // linux
	return CPair<u32, u32>(1, 1);
#endif // WIN32
}

u32 CSystem::getCpuSpeed()
{
#ifdef WIN32
	HKEY t_key;
	LONG t_err = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &t_key);

	if (t_err != ERROR_SUCCESS)
		return 0;

	DWORD t_speed = 0;
	DWORD t_size = sizeof(t_speed);
	t_err = RegQueryValueExA(t_key, "~MHz", 0, 0, (LPBYTE)&t_speed, &t_size);

	RegCloseKey(t_key);

	if (t_err != ERROR_SUCCESS)
		return 0;

	return t_speed;
#else // linux
	return 0;
#endif // WIN32
}

stringc CSystem::getCpuName()
{
#ifdef WIN32
	HKEY t_key;
	LONG t_err = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &t_key);

	if (t_err != ERROR_SUCCESS)
		return "";

	char t_buffer[256];
	DWORD t_size = sizeof(t_buffer);
	t_err = RegQueryValueExA(t_key, "ProcessorNameString", 0, 0, (LPBYTE)&t_buffer, &t_size);

	RegCloseKey(t_key);

	if (t_err != ERROR_SUCCESS)
		return "";

	return t_buffer;
#else // linux
	return "";
#endif // WIN32
}

bool CSystem::getClipboard(char *_dest, u32 _destSize)
{
#ifdef WIN32
	if (OpenClipboard(0))
	{
		HANDLE t_cbhandle = GetClipboardData(CF_TEXT);
		const char *t_text = (const char*)GlobalLock(t_cbhandle);
		GlobalUnlock(t_cbhandle);
		CloseClipboard();
		xstrcpy(_dest, _destSize, t_text);
		return true;
	}
#else // linux
	//...
#endif // WIN32
	return false;
}

bool CSystem::setClipboard(const char *_str)
{
#ifdef WIN32
	if (OpenClipboard(0))
	{
		EmptyClipboard();
		HANDLE t_buf = GlobalAlloc(GMEM_DDESHARE, strlen(_str) + 1);
		char *t_text = (char*)GlobalLock(t_buf);
		xstrcpy(t_text, 2147483647, _str);
		GlobalUnlock(t_buf);
		SetClipboardData(CF_TEXT, t_buf);
		CloseClipboard();
		return true;
	}
#else // linux
	//...
#endif // WIN32
	return false;
}

stringc CSystem::getUserName()
{
#ifdef WIN32
	static char t_buffer[256];
	DWORD t_buf_size = sizeof(t_buffer) / sizeof(char);
	if (GetUserNameA(t_buffer, &t_buf_size))
	{
		return t_buffer;
	}
#else // linux
	//...
#endif // WIN32
	return "";
}

u32 CSystem::getTime()
{
#ifdef WIN32
	//return timeGetTime();
	return 0;
#else // linux
	//...
#endif // WIN32
}

void CSystem::exit(s32 _code)
{
	::exit(_code);
}

s32 CSystem::messageBox(const char *_caption, const char *_message)
{
#ifdef WIN32
	return MessageBoxA(0, _message, _caption, MB_OK);
#else // linux
	return 0;
#endif // WIN32
}